import { applyMiddleware, createStore, compose } from "redux";
import { createLogger } from "redux-logger";
import reduxPromise from "../midware/promise";
import reducerProcessor from "./reducer";

export default async options => {
  const { plugin } = options;
  const composeEnhancers =
    (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
  const middleware = [reduxPromise, ...Object.values(plugin)] as any;
  let processer = compose;
  if (process.env.NODE_ENV !== "production") {
    middleware.push(createLogger());
    processer = composeEnhancers;
  }

  return reducerProcessor(options).then(reducer => {
    return preloadedStore => {
      return createStore(
        reducer,
        preloadedStore,
        processer(applyMiddleware(...middleware))
      );
    };
  });
};
